/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permuteUnique = function (nums) {
  let res = [], arr = [], visited = new Array(nums.length).fill(0)
  bc(0)

  function bc(index) {
    if (arr.length === nums.length) {
      res.push([...arr])
      return
    }

    for (let i = 0; i < nums.length; i++) {
      if (visited[i] == 1 || (i != 0 && nums[i] == nums[i - 1] && visited[i]==1)) continue
      visited[i] = 1
      arr.push(nums[i])
      bc(i + 1)
      arr.pop()
      visited[i] = 0
    }
  }

  return res
};

permuteUnique([1, 1, 2])